package com.qingxin.park.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.qingxin.park.entity.Cropsyz;
import com.qingxin.park.entity.ProductionyzLink;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;

/**
 * 农作物生产环节
 * @author Administrator
 *
 */
@Service
public class ProductionyzLinkService extends BaseService<ProductionyzLink, String> {

	
	/**
	 * 获取作物生产环节数据
	 * @param cropid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getProlinkData(String cropid, Integer type){
		List<Map<String, Object>> list = new ArrayList<>();
		
		StringBuilder hql = new StringBuilder();
		hql.append("select new com.qingxin.park.entity.ProductionyzLink(p.linkid, p.linkName, p.linkType)");
		hql.append(" from ProductionyzLink p where p.crops.cropid=?");
		if(type != null){
			hql.append(" and p.linkType="+type+" ");
		}
		hql.append(" order by p.linkName asc");
		List<ProductionyzLink> links = listByHql(hql.toString(), cropid);
		if(links != null){
			for(ProductionyzLink link : links){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", link.getLinkid());
				map.put("name", link.getLinkName());
				map.put("type", link.getLinkType());
				list.add(map);
			}
		}
		return list;
	}
	
	
	/**
	 * 获取图标
	 * @param linkid
	 * @return
	 */
	public String getProlinkIcon(String linkid){
		return (String) getByHql("select icon from ProductionyzLink where linkid=?", linkid);
	}
	
	
	
	/**
	 * 列表数据
	 * @param cropid
	 * @param linkName
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(String cropid, String linkName, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.park.entity.ProductionyzLink(" +
				"p.linkid, p.linkCode, p.linkName, p.linkType, p.sort, c.cropName)" +
				" from ProductionyzLink p, Cropsyz c where p.crops.cropid=c.cropid");
		
		querier.append(" and p.crops.cropid=:cropid");
		querier.setParam("cropid", cropid);
		
		if(StringUtils.isNotEmpty(linkName)){
			linkName = linkName.replace("'", "");
			querier.append(" and (p.linkName like :linkName or p.linkCode like :linkCode) ");
			querier.setParam("linkName", "%"+linkName+"%");
			querier.setParam("linkCode", "%"+linkName+"%");
		}
		
		querier.append(pageParam.getOrderby(" order by p.sort asc", "p"));
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 添加农作物生产环节信息
	 * @param link
	 * @param cropid
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(ProductionyzLink link, String cropid, LoginUser loginUser, HttpServletRequest request){
		link.setCrops(new Cropsyz(cropid));//不确定
		
		Date now = new Date();
		link.setCreateTime(now);
		link.setUpdateTime(now);
		link.setCreateBy(loginUser.getLogin_name());
		link.setUpdateBy(loginUser.getLogin_name());
		
		if(StringUtils.isNotEmpty(link.getIcon())){
			UploadFile.moveTmpTo(link.getIcon(), UploadFile.DIR_PROLINK_IMG);
		}
		
		this.save(link);
		return null;
	}
	
	
	/**
	 * 修改农作物生产环节信息
	 * @param link
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(ProductionyzLink link, LoginUser loginUser, HttpServletRequest request){
		ProductionyzLink old = getSafeEntity(link.getLinkid());
		if(old == null){
			return new SysError("参数错误");
		}
		
		if(StringUtils.isNotEmpty(link.getIcon()) && !link.getIcon().equals(old.getIcon())){
			UploadFile.moveTmpTo(link.getIcon(), UploadFile.DIR_PROLINK_IMG);
		}
		
		String fields = "linkCode,linkName,linkType,sort,icon,remark";
		BeanUtil.setTargetFields(old, link, fields, true);
		
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		
		Updater<ProductionyzLink> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		
		return null;
		
	}
	
	
	/**
	 * 删除农作物生产环节信息
	 * @param linkids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] linkids, LoginUser loginUser, HttpServletRequest request){
		for(String linkid : linkids){
			ProductionyzLink link = get(linkid);
			if(link==null){
				continue;
			}
			
			this.delete(link);
		}
		
		return null;
	}
	
	
}
